package cn.edu.ncut.cs.springboot.springsecuritydemo.controller;

import cn.edu.ncut.cs.springboot.springsecuritydemo.entity.Attendance;
import cn.edu.ncut.cs.springboot.springsecuritydemo.service.AttendanceService;
import cn.edu.ncut.cs.springboot.springsecuritydemo.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/wode/system")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    // 获取所有考勤信息
    @GetMapping
    @PreAuthorize("hasAnyRole('HR', 'ADMIN','NORMAL_USER')")
    public JsonResult getAllAttendances() {
        List<Attendance> attendances = attendanceService.getAllAttendances();
        return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "All Attendances", attendances);
    }

    // 根据ID获取考勤信息
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('HR', 'ADMIN','NORMAL_USER')")
    public JsonResult getAttendanceById(@PathVariable Long id) {
        Optional<Attendance> attendance = attendanceService.getAttendanceById(id);
        return attendance.map(value -> new JsonResult(JsonResult.JsonResultCode.SUCCESS, "Attendance Found", value))
                .orElseGet(() -> new JsonResult(JsonResult.JsonResultCode.ERROR, "Attendance Not Found", null));
    }

    // 根据员工ID获取考勤信息
    @GetMapping("/employee/{employeeId}")
    @PreAuthorize("hasAnyRole('HR', 'ADMIN','NORMAL_USER')")
    public JsonResult getAttendancesByEmployeeId(@PathVariable Long employeeId) {
        List<Attendance> attendances = attendanceService.getAttendancesByEmployeeId(employeeId);
        return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "Attendances for Employee", attendances);
    }

    // 新增考勤信息
    @PostMapping("/kaoqin/add")
    @PreAuthorize("hasAnyRole('HR', 'ADMIN')")
    public JsonResult createAttendance(@RequestBody Attendance attendance) {
        try {
            Attendance createdAttendance = attendanceService.createAttendance(attendance);
            return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "Attendance Created Successfully", createdAttendance);
        } catch (Exception e) {
            return new JsonResult(JsonResult.JsonResultCode.ERROR, "Failed to Create Attendance: " + e.getMessage(), null);
        }
    }

    // 修改考勤信息
    @PutMapping("/kaoqin/change/{id}")
    @PreAuthorize("hasAnyRole('HR', 'ADMIN')")
    public JsonResult updateAttendance(@PathVariable Long id, @RequestBody Attendance attendance) {
        try {
            attendance.setId(id);
            Attendance updatedAttendance = attendanceService.updateAttendance(attendance);
            if (updatedAttendance != null) {
                return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "Attendance Updated Successfully", updatedAttendance);
            } else {
                return new JsonResult(JsonResult.JsonResultCode.ERROR, "Attendance Not Found", null);
            }
        } catch (Exception e) {
            return new JsonResult(JsonResult.JsonResultCode.ERROR, "Failed to Update Attendance: " + e.getMessage(), null);
        }
    }

    // 删除考勤信息
    @DeleteMapping("/kaoqin/delete/{id}")
    @PreAuthorize("hasAnyRole('HR', 'ADMIN')")
    public JsonResult deleteAttendance(@PathVariable Long id) {
        try {
            attendanceService.deleteAttendance(id);
            return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "Attendance Deleted Successfully", null);
        } catch (Exception e) {
            return new JsonResult(JsonResult.JsonResultCode.ERROR, "Failed to Delete Attendance: " + e.getMessage(), null);
        }
    }
}
